﻿using System;
using RayD.Repository.Entites.SystemModule;
using System.Collections.Generic;
using System.Text;
using RayD.Repository;
using RayD.Core;
using Dapper;
using System.Data;

namespace RayD.App.SystemModule
{
    public class DatabaseTableApp
    {
        #region 构造函数注入
        private IRepository BaseRepository;
        public DatabaseTableApp(IRepository repository)
        {
            this.BaseRepository = repository;
        }
        #endregion

        #region 获取数据

        /// <summary>
        /// 数据表列表
        /// </summary>
        /// <returns></returns>
        public IEnumerable<DatabaseTableModel> GetTableList()
        {
            return BaseRepository.GetDBTable<DatabaseTableModel>();
        }

        /// <summary>
        /// 数据表字段列表
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <returns></returns>
        public IEnumerable<DatabaseTableFieldModel> GetTableFiledList(string tableName)
        {
            return BaseRepository.GetDBTableFields<DatabaseTableFieldModel>(tableName);
        }
        #endregion

        #region 提交数据
        #endregion

        #region 扩展方法
        /// <summary>
        /// C#实体数据类型
        /// </summary>
        /// <param name="datatype">数据库字段类型</param>
        /// <returns></returns>
        public string FindModelsType(string datatype)
        {
            string res = "string";
            datatype = datatype.ToLower();
            switch (datatype)
            {
                case "int":
                case "number":
                case "integer":
                case "smallint":
                    res = "int?";
                    break;
                case "tinyint":
                    res = "byte?";
                    break;
                case "numeric":
                case "real":
                case "float":
                case "decimal":
                case "number(8,2)":
                case "money":
                case "smallmoney":
                    res = "decimal?";
                    break;
                case "char":
                case "varchar":
                case "nvarchar2":
                case "text":
                case "nchar":
                case "nvarchar":
                case "ntext":
                    res = "string";
                    break;
                case "bit":
                    res = "bool?";
                    break;
                case "datetime":
                case "date":
                case "smalldatetime":
                    res = "DateTime?";
                    break;
                default:
                    res = "string";
                    break;
            }
            return res;
        }
        #endregion

    }
}
